package com.zjj.edu.web.controller.system;

import com.zjj.edu.common.annotation.Log;
import com.zjj.edu.common.constant.UserConstants;
import com.zjj.edu.common.core.controller.BaseController;
import com.zjj.edu.common.core.domain.AjaxResult;
import com.zjj.edu.common.core.page.TableDataInfo;
import com.zjj.edu.common.core.text.Convert;
import com.zjj.edu.common.enums.BusinessType;
import com.zjj.edu.common.utils.poi.ExcelUtil;
import com.zjj.edu.framework.util.ShiroUtils;
import com.zjj.edu.framework.util.UserPermissionUtil;
import com.zjj.edu.system.domain.SysCampus;
import com.zjj.edu.system.domain.SysTenant;
import com.zjj.edu.system.domain.SysUser;
import com.zjj.edu.system.service.ISysCampusService;
import com.zjj.edu.system.service.ISysTenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 校区Controller
 *
 * @author zjj
 * @date 2020-06-02
 */
@Controller
@RequestMapping("/system/campus")
public class SysCampusController extends BaseController
{
    private String prefix = "system/campus";

    @Autowired
    private ISysCampusService sysCampusService;

    @Autowired
    private ISysTenantService tenantService;

    @RequiresPermissions("system:campus:view")
    @GetMapping()
    public String campus(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/campus";
    }

    /**
     * 查询校区列表
     */
    @RequiresPermissions("system:campus:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysCampus sysCampus)
    {
        startPage();
        sysCampus.setTenantId(ShiroUtils.getTenantId());
        List<SysCampus> list = sysCampusService.selectSysCampusList(sysCampus);
        return getDataTable(list);
    }

    /**
     * 导出校区列表
     */
    @RequiresPermissions("system:campus:export")
    @Log(title = "校区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysCampus sysCampus)
    {
        sysCampus.setTenantId(ShiroUtils.getTenantId());
        List<SysCampus> list = sysCampusService.selectSysCampusList(sysCampus);
        ExcelUtil<SysCampus> util = new ExcelUtil<SysCampus>(SysCampus.class);
        return util.exportExcel(list, "campus");
    }

    /**
     * 新增校区
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/add";
    }

    /**
     * 新增保存校区
     */
    @RequiresPermissions("system:campus:add")
    @Log(title = "校区", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysCampus sysCampus)
    {
        long tenantId = ShiroUtils.getTenantId();
        sysCampus.setTenantId(tenantId);
        int dbCampusNum = sysCampusService.selectCampusNum(tenantId);

        SysTenant tenant = tenantService.selectTenantById(tenantId);
        int campusNumLimit = Integer.parseInt(tenant.getCampusNumLimit());
        if (campusNumLimit !=0 && (dbCampusNum+1) > campusNumLimit) {
            return error("新增校区已达当前上限，请联系平台管理员开通！");
        }
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysCampusService.checkNameUnique(sysCampus))) {
            return error("新增校区'" + sysCampus.getName() + "'失败，名称已存在");
        }
        sysCampus.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysCampusService.insertSysCampus(sysCampus));
    }

    /**
     * 校验名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkTenantNameUnique(SysCampus sysCampus) {
        sysCampus.setTenantId(ShiroUtils.getTenantId());
        return sysCampusService.checkNameUnique(sysCampus);
    }

    /**
     * 校区详情
     */
    @RequiresPermissions("system:campus:details")
    @GetMapping("/details/{id}")
    public String details(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("sysCampus", sysCampusService.selectSysCampusById(id));
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/details";
    }

    /**
     * 修改校区
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("sysCampus", sysCampusService.selectSysCampusById(id));
        mmap.put("tenantList", tenantService.selectTenantAll());
        return prefix + "/edit";
    }

    /**
     * 修改保存校区
     */
    @RequiresPermissions("system:campus:edit")
    @Log(title = "校区", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysCampus sysCampus)
    {
        sysCampus.setTenantId(ShiroUtils.getTenantId());
        if (UserConstants.NAME_NOT_UNIQUE.equals(sysCampusService.checkNameUnique(sysCampus))) {
            return error("修改校区'" + sysCampus.getName() + "'失败，名称已存在");
        }
        sysCampus.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysCampusService.updateSysCampus(sysCampus));
    }

    /**
     * 删除校区
     */
    @RequiresPermissions("system:campus:remove")
    @Log(title = "校区", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SysUser user = ShiroUtils.getSysUser();
        SysCampus campus = new SysCampus();
        campus.setIds(Convert.toStrArray(ids));
        campus.setUpdateBy(user.getUserName());
        if (!UserPermissionUtil.checkUserPermiss()) {
            campus.setTenantId(user.getTenantId());
        }
        return toAjax(sysCampusService.deleteSysCampusByIds(campus));
    }

    /**
     * 状态修改
     */
    @Log(title = "校区管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:campus:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysCampus sysCampus)
    {
        sysCampus.setTenantId(ShiroUtils.getTenantId());
        return toAjax(sysCampusService.updateSysCampus(sysCampus));
    }

}
